<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // //定义一个现在的时间
        // let cDate = new Date();
        // document.write(cDate);

        // //定义一个过去的时间
        // let preDate = new Date('2000/08/29 08:08:00');
        // document.write('<br>',preDate);

        // //定义一个将来的时间
        // let nextDate = new Date('2022/08/29 08:08:00');
        // document.write('<br>',nextDate);

        // //常用的方法
        // let date = new Date();
        // console.log(date.getDate());//日
        // console.log(date.getDay());//星期几
        // console.log(date.getMonth()+1);//月
        // console.log(date.getFullYear());//年份
        // console.log(date.getHours());//小时
        // console.log(date.getMinutes());//分钟
        // console.log(date.getSeconds());//秒
        // console.log(date.getMilliseconds());//毫秒
        // console.log(date.getTime());//时间数，1970年1月1日至今的毫秒数

        let curr = formatDate(new Date());
        console.log(`当前是时间是${curr}`);

        function formatDate(date){
            // 如果data不是Data对象，返回空
            if(!date instanceof Date){
                return;
            }

            let year = date.getFullYear();
            let mouth = date.getMonth()+1;
            let day = date.getDate();
            let hour = date.getHours();
            let minute = date.getMinutes();
            let second = date.getSeconds();
            console.log(year,mouth,day,hour,minute,second);

            //格式转换
            mouth = mouth<10?'0'+mouth:mouth;
            day = day<10?'0'+day:day;
            hour = hour<10?'0'+hour:hour;
            minute = minute<10?'0'+minute:minute;
            second = second<10?'0'+second:second;

            return `${year}年-${mouth}月-${day}日-${hour}时-${minute}分-${second}秒`;
        }
    </script>
</body>
</html>